<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="ae.pcfc.etks.online.itworx.ehs.web.web.util.ViewUtils"%>
<%@ include file="/WEB-INF/view/include.jsp"%>
<%@page
	import="ae.pcfc.etks.online.itworx.ehs.web.constants.GlobalConstants"%>
<script>

window.onload = function(){

	new JsDatePick({

		useMode:2,

		target:"inputField1",

		dateFormat:"%d %M %Y"

	});
	
};
	//Clear Date

$('span.clear_expiry_date').live('click', function(){

	$('input#inputField1').val("");

});

	function submitFnpForm(formId) {
		
		var selObj = document.getElementById('fnpServices');
		var dateField= document.getElementById("inputField1");
		
		if(formId == 'fnpParametersFrm' && dateField.value != "" )
		{
			var currentTime = new Date();
			currentTime.setHours(00);
			currentTime.setMinutes(00);
			currentTime.setSeconds(00);
			currentTime.setMilliseconds(00);

			var selectedTime= new Date (dateField.value);
			selectedTime.setHours(00);
			selectedTime.setMinutes(00);
			selectedTime.setSeconds(00);
			selectedTime.setMilliseconds(00);
			
			if(selectedTime.getTime() <currentTime.getTime())
			{
				alert('<spring:message code="error.fnp.invalid.date" />');
				return;
			}
		}

		if(selObj.selectedIndex >-1)
			{
				document.forms[formId].submit();
			}
		else
			{
				alert('<spring:message code="error.fnp.service.required" />');
			}
	}

	function sbumitParent() {
		
		var fnpDetailsElement = $("#fnpDetails", parent.document);
		var fnpFeesElement = $("#fnpFees", parent.document);
		
		fnpDetailsElement.val(document.getElementById("fnpJSON").value);
		fnpFeesElement.val(document.getElementById("fees").value);
		parent.hideShowApproveButton();
		parent.$.fn.colorbox.close();
	}

	function closeChild() {
		window.close();
	}

	function checkIt(evt) {
	    evt = (evt) ? evt : window.event;
	    var charCode = (evt.which) ? evt.which : evt.keyCode;
	    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
	        status = "This field accepts numbers only.";
	        return false;
	    }
	    status = "";
	    return true;
	}
</script>
<div class="popup_container">

	<form:form id="fnpServicesFrm" modelAttribute="fnp" method="POST"
		action="fnp/fnpMapping.htm?type=feesMapping">

		<div style="color:#ED0000;font-size:12px;margin:0 0 10px 10px" id="errorDiv">
			<spring:bind path="fnp.*">
				<c:forEach items="${errors.globalErrors}" var="error">
					<c:out value="${error}" />
					<br />
				</c:forEach>
			</spring:bind>
		</div>
		
		<div style="color:#ED0000;font-size:12px;margin:0 0 10px 10px" id="errorDiv">
			<spring:bind path="fnp.*">
				<c:forEach items="${status.errorMessages}" var="error">
					<c:out value="${error}" />
					<br />
				</c:forEach>
			</spring:bind>
		</div>

		<form:hidden path="userId" />
		<form:hidden path="requestId" />
		<fieldset class="clear">
			<legend>
				<spring:message code="label.officer.service.list.label" />
			</legend>
		</fieldset>

		<div class="t_form-left">
			<label><spring:message
					code="label.officer.service.list.label" />:</label>

			<form:select id="fnpServices" path="fnpServices" multiple="true" size="5"
				class="multiple">
				<c:forEach var="fnpService" items="${fnpAllServices}">
					<c:set var="isSelected" value="false"/>
					<c:forEach items="${fnp.fnpServices}" var="selectedService">
						<c:if test="${selectedService.serviceId eq fnpService.serviceId}">
						    <c:set var="isSelected" value="true" />
						 </c:if>
					</c:forEach>
										
	        		<option value="${fnpService.serviceId}"  <c:if test="${isSelected}">selected="selected"</c:if> title="${fnpService.fullServiceName}"><c:out value="${fnpService.fullServiceName}"/></option>
	    		</c:forEach>
    		
			</form:select>

			<div class="popup_btn">

				<span class="btn_border">

					<button type="button" class="btn_bg" id="GetParameters"
						onclick="submitFnpForm('fnpServicesFrm');">
						<spring:message code="label.officer.btn.get.parameters.label" />
					</button> </span>

			</div>

		</div>

	</form:form>

	<div class="brk"></div>

	<form:form id="fnpParametersFrm"
		action="fnp/calculate.htm?type=feesMapping" modelAttribute="fnp"
		method="POST">

		<form:hidden path="userId" />
		<form:hidden path="requestId" />

		<c:if test="${fnp.parametersExists}">
			<fieldset class="clear">

				<legend>
					<spring:message code="label.officer.service.label" />
				</legend>

			</fieldset>

			<div class="t_form-left">

				<c:forEach items="${fnp.fnpServices}" var="service"
					varStatus="serviceStatus">

					<table class="grdView clear">
						<caption></caption>
						<colgroup>
							<col width="50" />
							<col width="50%" />
						</colgroup>
						<tr>
							<th><spring:message code="fnp.parameter.name"/></th>
							<th><spring:message code="fnp.parameter.value"/></th>
						</tr>


						<tr>
							<td><spring:message code="fnp.service.quantity"/>:</td>
							<td><input
									name="fnpServices[${serviceStatus.index}].quantity"
									type="text" class="fnp_form-fld" value="${service.quantity}"
									maxlength="10" onkeypress="return checkIt(event);"/>
							</td>
						</tr>
							
						<c:forEach items="${service.fnpParameters}" var="parameter"
							varStatus="status">

							<c:if test="${status.index == 0}">
									<input type="hidden"
										name="fnpServices[${serviceStatus.index}].serviceId"
										value="${parameter.serviceId}" />
							</c:if>
							<c:choose>
								<c:when test="${parameter.dummy == false}">
								<tr>
										<c:choose>
											<c:when
												test='${requestScope.requestContextAttribute.locale.language == "ar"}'>
												<td><c:out value="${parameter.arabicName}" escapeXml="true"/>:</td>
											</c:when>
											<c:otherwise>
												<td><c:out value="${parameter.englishName}" escapeXml="true"/>:</td>
											</c:otherwise>
										</c:choose>
		
										<td><input
											name="fnpServices[${serviceStatus.index}].fnpParameters[${status.index}].value"
											type="text" class="fnp_form-fld" value="${parameter.value}"
											maxlength="50" />
										</td>
										<input type="hidden"
											name="fnpServices[${serviceStatus.index}].fnpParameters[${status.index}].serviceId"
											value="${parameter.serviceId}" />
										<input type="hidden"
											name="fnpServices[${serviceStatus.index}].fnpParameters[${status.index}].order"
											value="${parameter.order}" />
										<input type="hidden"
											name="fnpServices[${serviceStatus.index}].fnpParameters[${status.index}].name"
											value="${parameter.name}" />
										<c:choose>
											<c:when
												test='${requestScope.requestContextAttribute.locale.language == "ar"}'>
												<input type="hidden"
													name="fnpServices[${serviceStatus.index}].fnpParameters[${status.index}].arabicName"
													value="${parameter.arabicName}" />
											</c:when>
											<c:otherwise>
												<input type="hidden"
													name="fnpServices[${serviceStatus.index}].fnpParameters[${status.index}].englishName"
													value="${parameter.englishName}" />
											</c:otherwise>
										</c:choose>
										</tr>
								</c:when>
							<c:otherwise>
								<input type="hidden"
											name="fnpServices[${serviceStatus.index}].fnpParameters[${status.index}].serviceId"
											value="${parameter.serviceId}" />
								<input type="hidden"
											name="fnpServices[${serviceStatus.index}].fnpParameters[${status.index}].dummy"
											value="true" />
								<input type="hidden"
											name="fnpServices[${serviceStatus.index}].fnpParameters[${status.index}].name"
											value="${parameter.name}" />
								<tr>
									<td colspan="2">
										<div class="error_dark"><normal><spring:message code="label.fnp.no.parameters.exist"/></normal></div>
									</td>
								</tr>
							</c:otherwise>
							</c:choose>
							
							</c:forEach>
					</table>
					<div class="brk"></div>
				</c:forEach>
				<label><spring:message
						code="label.officer.expiry.date.label" />:</label>
				<form:input path="expiryDate" size="12" id="inputField1"
					class="t_form-fld-disable_calendar"
					title="Click here to choose day" ReadOnly="true" />
				<span class="clear_expiry_date"> <img
					src="resources/images/edit-clear.png" width="16px" height="16px"
					title="<spring:message code="label.img.field.removeDate"/>" /></span>
				<label><spring:message code="label.request.requestId"/>:</label>
				<form:input readonly="true" path="formattedRequestId" />				
				<label><spring:message code="label.officer.remarks.label" />:</label>
				<form:textarea path="remarks" cols="22" rows="2" />

				<div class="brk"></div>
				<c:if test="${not empty fnpFees}">
					<label id="total_fees"><strong><spring:message
								code="label.officer.total.fees.label" />: </strong> </label>
					<input type="text" id="fees" value="${fnpFees}" readonly="readonly" /><span class="fees_currency"><spring:message code="fnp.fees.currency" /></span> 
				</c:if>

				<div class="popup_btn">
					<input type="hidden" id="fnpJSON"
						value="<c:out value="${fnpJSON}" escapeXml="true"/>" /> <span
						class="btn_border">

						<button type="button" class="btn_bg" id="RetrieveFees"
							onclick="submitFnpForm('fnpParametersFrm');">
							<spring:message code="label.officer.btn.calculate.fees.label" />
						</button> </span>

					<c:if test="${not empty fnpFees}">
						<span class="btn_border" id="save_and_close">

							<button type="button" class="btn_bg" id="RetrieveFeesBack"
								onclick="sbumitParent();">
								<spring:message code="label.officer.btn.save.close.label" />
							</button> </span>
					</c:if>
				</div>
			</div>
		</c:if>
	</form:form>
	<div>